﻿using System.Web.Mvc;
using LibraryHub.BusinessLogic;
using LibraryHub.Core.Models.Book;

namespace LibraryHub.Controllers
{
    public class BookController : OneUserController
    {
        private readonly BooksBl bl = new BooksBl();

        #region Index

        public ActionResult Index()
        {
            var model = bl.Index();

            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                return View("IndexForLibrarian", model);
            }

            #endregion

            return View(model);
        }

        #endregion

        #region Create

        public ActionResult Create()
        {
            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                return View();
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        [HttpPost]
        public ActionResult Create(BookModel book)
        {
            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                bool success = bl.CreateBook(book);
                if (success)
                {
                    return RedirectToAction("Index");
                }

                return View(book);    
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        #endregion

        #region Edit

        public ActionResult Edit(int id) //TODO: no error handling. Opening /Edit/ (no number) will crash application.
        {
            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                var model = bl.FindBook(id);
                return View(model);
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        [HttpPost]
        public ActionResult Edit(BookModel book)
        {
            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                bool success = bl.EditBook(book);
                if (success)
                {
                    return RedirectToAction("Index");
                }
                return View(book);                
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        #endregion

        #region Details

        public ViewResult Details(int id) //TODO: no error handling.
        {
            var model = bl.FindBook(id);

            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                return View("DetailsForLibrarian", model);
            }

            #endregion

            return View(model);
        }

        #endregion

        #region TakeBook

        public ActionResult TakeBook(int id) //TODO: no error handling.
        {
            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                var model = new TakeBookModel();
                var book = bl.FindBook(id);
                model.Book = book;

                return View(model);
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        [HttpPost]
        public ActionResult TakeBook(TakeBookModel takeBook)
        {
            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                bool success = bl.TakeBook(takeBook);
                if (success)
                {
                    return RedirectToAction("Index");
                }
                return View(takeBook);
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        #endregion

        #region ReturnBook

        public ActionResult ReturnBook(int id) //TODO: no error handling.
        {
            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                var model = bl.GetReturnBookInfo(id);

                if (model == null)
                {
                    return RedirectToAction("AccessDenied");
                }

                return View(model);
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        [HttpPost, ActionName("ReturnBook")]
        public ActionResult ReturnBookConfirmed(int id)
        {
            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                bool success = bl.ReturnBook(id);
                if (success)
                {
                    return RedirectToAction("Index");
                }
                return View(id);
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        #endregion

        #region Delete

        public ActionResult Delete(int id) //TODO: no error handling.
        {
            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                var model = bl.FindBook(id);
                return View(model);                
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                bool success = bl.DeleteBook(id);
                if (success)
                {
                    return RedirectToAction("Index");
                }
                return View(id);                
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        #endregion
    }
}